package com.cn.fileTest;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * 文件的常用方法
 */
public class FileTest {
    @Test
    public void test1(){
        File file = new File("src/main/resources/test.txt");
        try {
            boolean created = file.createNewFile();
            System.out.println("File created: " + created);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件的创建
     */
    @Test
    public void test2(){
        File file = new File("src/main/resources/test2.txt");
        try {
            boolean created = file.createNewFile();
            System.out.println("File created: " + created);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 目录创建
     * 创建一个目录（仅创建单级目录）。
     */
    @Test
    public void test4(){
        File dir = new File("test2");
        boolean created = dir.mkdirs();
        System.out.println("Directory created: " + created);
    }


    /**
     * 目录创建
     * 创建目录，包括必要的父目录（支持多级目录）。
     */
    @Test
    public void test5(){
        File dir = new File("src/main/resources/test2");
        boolean created = dir.mkdirs();
        System.out.println("Directory created: " + created);
    }

    /**
     * 删除文件或空目录。
     */
    @Test
    public void test6(){
        // 文件
        File file = new File("src/main/resources/test2.txt");
        boolean deleted = file.delete();
        System.out.println("File deleted: " + deleted);

        // 目录
        File dir = new File("src/main/resources/test2");
        boolean deleted1 = dir.delete();
        System.out.println("File deleted: " + deleted);
    }


}
